package com.cb.controller;

import com.cb.common.BaseContext;
import com.cb.common.R;
import com.cb.entity.ShoppingCart;
import com.cb.service.ShoppingcartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingcartService shoppingcartService;

    /**
     * 商品添加到购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}", shoppingCart);
        ShoppingCart shoppingCart1=shoppingcartService.add(shoppingCart);
        return R.success(shoppingCart1);
    }

    /**
     * 商品数量减少
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("数据：{}", shoppingCart);
        ShoppingCart shoppingCart1=shoppingcartService.sub(shoppingCart);
        return R.success(shoppingCart1);
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        List<ShoppingCart> shoppingCarts=shoppingcartService.getCart();
        return R.success(shoppingCarts);
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        shoppingcartService.clean();
        return R.success("清空购物车成功！");
    }

}
